<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>查询</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 表单查询 -->
    <el-card>
      <el-table :data="userlist" stripe style="width: 100%">>
        <el-table-column prop="rel_id" label="关系ID" width="400">
        </el-table-column>
        <el-table-column prop="rel_one" label="成员1" width="400">
        </el-table-column>
        <el-table-column prop="rel_two" label="成员2">
        </el-table-column>
        <el-table-column prop="relationship" label="关系">
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 修改对话框 -->
    <el-dialog :visible.sync="updataDialogVisible" width="30%">
      <el-form :model="formvalue" label-width="80px">
        <el-form-item label="关系ID">
          <el-input v-model="formvalue.rel_id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="成员1">
          <el-input v-model="formvalue.rel_one"></el-input>
        </el-form-item>
        <el-form-item label="成员2">
          <el-input v-model="formvalue.rel_two"></el-input>
        </el-form-item>
        <el-form-item label="关系">
          <el-input v-model="formvalue.relationship"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updataDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除对话框 -->
    <el-dialog :visible.sync="deleteDialogVisible" width="30%">
      <el-form :model="formvalue" label-width="80px">
        <el-form-item label="关系ID">
          <el-input v-model="formvalue.rel_id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="成员1">
          <el-input v-model="formvalue.rel_one"></el-input>
        </el-form-item>
        <el-form-item label="成员2">
          <el-input v-model="formvalue.rel_two"></el-input>
        </el-form-item>
        <el-form-item label="关系">
          <el-input v-model="formvalue.relationship"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dleteUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userlist: [],
      updataDialogVisible: false,
      deleteDialogVisible: false,
      formvalue: {}
    }
  },
  created() {
    this.getUserlist()
  },
  methods: {
    // 获取用户的信息列表
    async getUserlist() {
      const { data: res } = await this.$http.get('relation')
      this.userlist = res.data.value
    },
    // 展示用户的修改对话框
    showUpdataDialog(rowvalue) {
      this.updataDialogVisible = true
      this.formvalue = rowvalue
    },
    // 发起修改请求
    async updataUser() {
      await this.$http.put('relation', {
        rel_id: this.formvalue.rel_id,
        rel_one: this.formvalue.rel_one,
        rel_two: this.formvalue.rel_two,
        relationship: this.formvalue.relationship
      })
      this.getUserlist()
      this.updataDialogVisible = false
    },
    // 展示删除对话框
    showDeleteDialog(rowvalue) {
      this.deleteDialogVisible = true
      console.log(rowvalue)
      this.formvalue = rowvalue
    },
    // 发起删除请求
    async dleteUser() {
      await this.$http.delete('relation?rel_id=' + this.formvalue.rel_id)
      this.getUserlist()
      this.deleteDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
